package tech.aistar.controller;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.DigestUtil;
import jakarta.servlet.http.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import tech.aistar.pojo.User;
import tech.aistar.service.UserService;
import tech.aistar.util.RegisterUtil;
import tech.aistar.vo.Result;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private  RegisterUtil registerUtil;


    @GetMapping("exit")
    public Result exit(){
        return new Result("200","退出成功");
    }


    @PostMapping("param01/{phone}")
    public Result update( String username, String intro, @PathVariable("phone")String phone) {
        userService.update(username,intro,phone);
        return new Result("200","修改成功");
    }
    @PostMapping("photo/{phone}")
    public Result update(@RequestBody String uPhoto ,@PathVariable("phone")String phone) {

        userService.updatePhoto(uPhoto,phone);
        return new Result("200","修改成功");
    }

    @PostMapping("param02")

    public Result findXinXi(@RequestBody User user) {

        List<User> users= userService.findXinXi();


        return new Result("200","查询成功",users);

    }
    @Autowired
    private  RedisTemplate redisTemplate;
//    @GetMapping
//    public String index(){return "/dev/views/slx/zhuce.html";}
    //登录验证码
    @PostMapping("code/{phone}")
    public Result code(@PathVariable String phone, HttpServletRequest request){
        String code=RegisterUtil.Code();
        ValueOperations vop = redisTemplate.opsForValue();
        vop.set("code",code, 60,TimeUnit.SECONDS);

        registerUtil.sendSms(phone,code);
        System.out.println(phone);
        System.out.println(code);
        HttpSession session = request.getSession();
        session.setAttribute(phone,code);


        return new Result("200","发送验证码成功",null);

    }
    //注册验证码
    @PostMapping("code1/{phone}")
    public Result code1(@PathVariable String phone, HttpServletRequest request){
        User user=userService.register(phone);
        String code=RegisterUtil.Code();
        ValueOperations vop = redisTemplate.opsForValue();
        vop.set("code",code, 60,TimeUnit.SECONDS);

        registerUtil.sendSms(phone,code);
        System.out.println(phone);
        System.out.println(code);
        HttpSession session = request.getSession();
        session.setAttribute(phone,code);
        if (user==null) {
            return new Result("200", "发送验证码成功", null);
        }else {
            return new Result("404", "手机号已存在可直接登录", null);
        }

    }
//注册
    @PostMapping("register/{phone}/{code}/{password}/{username}")
    public Result register(@PathVariable("phone") String phone ,@PathVariable("code") String code,@PathVariable("password") String password,@PathVariable("username") String username){
        System.out.println(phone);
        User user=userService.register(phone);
        ValueOperations vop = redisTemplate.opsForValue();
        vop.get("code");
        vop.get("password");
        if(user==null){

           String pwd=DigestUtil.md5Hex(password);
            userService.insert(phone,pwd,username);
            user= userService.register(phone);
            System.out.println("=2=");
//            String token = JWTUtils.geneJsonWebToken(user);
            return new Result("200","添加用户成功!",phone);
        }else if (vop.get("code")==null) {
            return new Result("404","验证码不存在");
        } else if (!code.equals(vop.get("code"))) {
            return new Result("404","验证码错误");
        } else {
            return new Result("500","其他错误");
        }
    }
//登录
    @PostMapping("login/{phone}/{code}")
    public Result login(@PathVariable("phone") String phone,@PathVariable("code") String code ,HttpServletResponse resp) {
        User user = userService.register(phone);
        ValueOperations vop = redisTemplate.opsForValue();
         String code1= (String) vop.get("code");
//        System.out.println(code+"------------------"+code1);
        if (user == null) {
            return new Result("404", "用户名不存在!,请先注册");
        } else if (vop.get("code") == null) {
            return new Result("404", "验证码不存在");
        } else if (!code.equals(vop.get("code"))) {
            return new Result("404", "验证码错误");
        } else {
            //服务器创建一个token
            Cookie cookie = new Cookie("loginInfo", ":");
            //设置cookie的作用域 - 域

            //url中出现/dev/cookie的请求,请求头信息中才会带上cookie
            cookie.setPath("/");
            //设置一些可选的属性
            //设置过期时间
            cookie.setMaxAge(0);//单位是s
//
//            //通过resp中的addCookie方法将这个cookie对象通过响应头信息发送到客户端
            resp.addCookie(cookie);
            String token = JWTUtils.geneJsonWebToken(user);
            HttpSession session = request.getSession();
            session.setAttribute("phone",phone);
//            System.out.println(phone+"=================999999999999990");
            return new Result("200", "登录成功", phone);
        }
    }
        @PostMapping("head/{phone}")
        public Result LookUserName (@PathVariable("phone") String phone){
            User user = userService.register(phone);
            return new Result("200", "显示头像", user);

        }

}
